package com.blockchainenergy.blockChain.impl;

import com.alibaba.fastjson.JSON;
import com.blockchainenergy.blockChain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @descriptions:
 * @data: 2021/11/4 23:45
 */
@Service
@Transactional
public class BlockChainServiceImpl implements BlockChainService {
    /**
     * @Description 获取主机IP和端口号
     * @Param
     * @return
     **/
    @Resource
    private ServerConfig serverConfig;

    /**
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @Description 获取链信息
     * @Param []
     **/
    @Override
    public Map<String, Object> getFullChain() throws Exception {
        BlockChain blockChain = BlockChain.getInstance();
        Map<String, Object> response = new HashMap<String, Object>();
        response.put("chain", blockChain.getChain());
        response.put("length", blockChain.getChain().size());
        return response;
    }

    /**
     * 获取链信息带hash
     *
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> getFullChainWithHash() throws Exception {
        List<Block> chain = BlockChain.getInstance().getChain();
        List<BlockWithHash> list = new ArrayList<>();
        for (Block block : chain) list.add(new BlockWithHash(block, new Encrypt().getSHA256(JSON.toJSONString(block))));
        // 移除创世区块
        list.remove(0);
        Map<String, Object> response = new HashMap<String, Object>();
        response.put("chain", list);
        response.put("length", list.size());
        return response;
    }

    @Override
    public Map<String, Object> getLast10BlockWithHash() throws Exception {
        List<Block> chain = BlockChain.getInstance().getChain();
        List<BlockWithHash> list = new ArrayList<>();
        int len = chain.size();
        for (int i = Math.max(len - 10, 1); i < len; i++) {
            Block block = chain.get(i);
            list.add(new BlockWithHash(block, new Encrypt().getSHA256(JSON.toJSONString(block))));
        }
        Map<String, Object> response = new HashMap<String, Object>();
        response.put("chain", list);
        response.put("length", list.size());
        return response;
    }


    @Override
    public Block getBlockByIndex(Integer index) throws Exception {
        return BlockChain.getInstance().getChain().get(index - 1);
    }

    @Override
    public void matrixToBlock(int[][] matrix) throws Exception {
        BlockChain blockChain = BlockChain.getInstance();
        for (int i = 0; i < 33; i++) {
            for (int j = 0; j < 33; j++) {
                Carbon carbon = new Carbon(i + 1, j + 1, (double) matrix[i][j], 0.0, 0D);
                blockChain.newCarbonBlock(carbon);
            }
            if (matrix[i][33] != 0) {
                // 34列
                Carbon carbon = new Carbon(i + 1, i + 1, -1.0, (double) matrix[i][33], 0D);
                blockChain.newCarbonBlock(carbon);
            }
        }
    }
}
